package com.wyix.controller;

import com.wyix.model.Book;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Arrays;
import java.util.List;

/**
 * @author wangdx
 * @since 2025/8/8
 */
@Controller
public class BookController {

    // 普通请求参数示例
//    @GetMapping("/books")
//    @ResponseBody
//    public String getBooks(
//            @RequestParam(defaultValue = "all") String type,
//            @RequestParam(defaultValue = "1") int page) {
//        return "查询【" + type + "】类图书，第" + page + "页结果";
//    }

    // 路径变量+矩阵变量示例
    @GetMapping("/books/{bookId}")
    @ResponseBody
    public String getBookDetail(
            @PathVariable String bookId,
            @org.springframework.web.bind.annotation.MatrixVariable(defaultValue = "list") String viewMode,
            @org.springframework.web.bind.annotation.MatrixVariable(defaultValue = "zh") String lang) {
        return "图书ID：" + bookId + "，显示模式：" + viewMode + "，语言：" + lang;
    }

    // 默认使用Thymeleaf视图（因优先级更高）
    @GetMapping("/books")
    public String listBooks(Model model) {
        List<Book> books = Arrays.asList(
                new Book(1, "《Spring实战》", "Craig Walls"),
                new Book(2, "《视图解析原理》", "《Views.md》作者")  // 呼应文档内容
        );
        model.addAttribute("books", books);
        return "book-list";  // 逻辑视图名
    }

    // 强制使用JSP视图（通过请求参数控制）
    @GetMapping("/books/jsp")
    public String listBooksWithJsp(Model model) {
        listBooks(model);  // 复用数据
        return "jsp:book-list";  // 前缀匹配JSP解析器
    }
}